#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2014年10月16日

@author: chenyongbing
'''
import ConfigParser,os,re
import base64
import sys
import logging

class LocalConfig(object):
    '''
    classdocs
    '''


    def __init__(self, config_file=None):
        self.config = ConfigParser.SafeConfigParser()
        if config_file == None:
            config_file = os.path.join(os.path.dirname(__file__),'conf/config.ini')
        self.config_file = config_file
        self.config.read(config_file)


        #self.log_config_file = self.config.get('log','config_file')
  
        #self.kafkaConfig = self.getConfigByFullName(section='kafka')
        self.kafkaConfig = self.getConfigByRegToDict(para='kafka.')
        #self.rawConfig = self.getConfigByFullName( section='rawdata')
        
        self.mysqlHosts = self.getConfigByRegToDict(para='mysql.')
      
      
    def getConfigByFullName(self,config=None,section=''):
        if config==None:
            config = self.config
        msg = {}
        for option in config.options(section):
            
            value = config.get(section,option)
            if option=='maxbytes':
                value = eval(value)
            elif re.search('count',option):
                value = int(value)
            elif option=='passwd':
                value = self.analysis_passwd(value)
            elif re.search('_path|_file',option):
                value = os.path.join(os.path.dirname(__file__),value)
            elif re.search('_list',option):
                value = value.split(',')
            msg[option] = value 
        return msg
  
    def getConfigByReg(self,config=None,para=''):
        if config==None:
            config = self.config
        msgs = []
        sections = config.sections()
        for section in sections:
            if not re.search(para,section):
                continue
            msg = {}
            for option in config.options(section):
                value = config.get(section,option)
                if option=='maxbytes':
                    value = eval(value)
                elif re.search('count',option):
                    value = int(value)
                elif option=='passwd':
                    value = self.analysis_passwd(value)
                elif re.search('_path|_file',option):
                    value = os.path.join(os.path.dirname(__file__),value)
                elif re.search('_list',option):
                    value = value.split(',')
                msg[option] = value
            if msg in msgs:continue
            msgs.append(msg)
        return msgs
    def getConfigByRegToDict(self,config=None,para=''):
        if config==None:
            config = self.config
        msgs = {}
        sections = config.sections()
        for section in sections:
            if not re.search(para,section):
                continue
            name = re.sub('%s\.*'%para,'',section)
            msg = {}
            for option in config.options(section):
                value = config.get(section,option)
                if option=='maxbytes':
                    value = eval(value)
                elif re.search('count',option):
                    value = int(value)
                elif option=='passwd':
                    value = self.analysis_passwd(value)
                elif re.search('_path|_file',option):
                    value = os.path.join(os.path.dirname(__file__),value)
                elif re.search('_list',option):
                    value = value.split(',')
                msg[option] = value

            msgs[name] = msg
        return msgs
        
    def analysis_passwd(self,passwd):
       
        new_passwd = base64.b64decode(passwd)
        return new_passwd
    def show_passwd(self,s,f):
        passwd = self.config.get(s,f)
        #passwd_lens = len(passwd)
        #passwd_lenx = passwd_lens - (passwd_lens % 4 if passwd_lens % 4 else 4)
        new_passwd = base64.b64decode(passwd)
#         new_passwd = base64.b64decode(passwd)
        return new_passwd
    
    def get_export_rule(self):
        config = ConfigParser.SafeConfigParser()
        config_file = os.path.join(os.path.dirname(__file__),'conf/export_rules.ini')
        config.read(config_file)
        sections = config.sections()
  
        self.rules = self.getConfigByRegToDict(config=config, para='rule.')
        
myLocalConfig = LocalConfig()
if __name__=="__main__":
    
    #print myLocalConfig.kafkaConfig['broker_list']
    #print myLocalConfig.rules
    print myLocalConfig.kafkaConfig

    #print myLocalConfig.remoteRedis